package mspbots.data.tl.prod.quickbooks.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.databind.JsonNode;
import lombok.extern.log4j.Log4j2;
import mspbots.data.tl.base.AbstractBaseServiceImpl;
import mspbots.data.tl.entity.ResultEntity;
import mspbots.data.tl.prod.quickbooks.mapper.QuickBooksDesktopCustomersMapper;
import mspbots.data.tl.prod.quickbooks.model.QuickBooksDesktopCustomers;
import mspbots.data.tl.prod.quickbooks.service.QuickBooksDesktopCustomersService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import java.time.LocalDateTime;

@Service
@Log4j2
@DS("master")
public class QuickBooksDesktopCustomersServiceImpl extends AbstractBaseServiceImpl<QuickBooksDesktopCustomersMapper, QuickBooksDesktopCustomers>
        implements QuickBooksDesktopCustomersService{

    /*
    {
        "BillAddressBlock":Object{...},
        "Email":"telltomson@yahoo.com",
        "IsActive":true,
        "FirstName":"Eltonjia",
        "ItemSalesTaxRef":Object{...},
        "EditSequence":1466113744,
        "Salutation":"Mrs",
        "BillAddress":Object{...},
        "Name":"4 Ever Young Daycare, Inc.",
        "TimeCreated":"2016-06-16T10:25:29-06:00",
        "TimeModified":"2016-06-23T11:46:15-06:00",
        "JobStatus":"None",
        "CompanyName":"4 Ever Young Day Care, Inc.",
        "TotalBalance":1813.41,
        "SalesTaxCodeRef":Object{...},
        "Sublevel":0,
        "ListID":"80000211-1466090729",
        "AdditionalContactRef":Array[3],
        "Phone":"(773) 966-6872",
        "FullName":"4 Ever Young Daycare, Inc.",
        "PreferredDeliveryMethod":"Email",
        "LastName":"Thompson",
        "Balance":0,
        "tenantId":1285403951449878530
    }
     */

    @Override
    public void sync(JsonNode node) {
        QuickBooksDesktopCustomers data = QuickBooksDesktopCustomers.builder()
                .name(super.getStringValue(node.get("Name")))
                .email(super.getStringValue(node.get("Email")))
                .fullName(super.getStringValue(node.get("FullName")))
                .lastName(super.getStringValue(node.get("LastName")))
                .firstName(super.getStringValue(node.get("FirstName")))
                .jobStatus(super.getStringValue(node.get("JobStatus")))
                .salutation(super.getStringValue(node.get("Salutation")))
                .phone(super.getStringValue(node.get("Phone")))
                .listId(super.getStringValue(node.get("ListID")))
                .isActive(super.getBooleanValue(node.get("IsActive")))
                .sublevel(super.getIntegerValue(node.get("Sublevel")))
                .syncTime(LocalDateTime.now())
                .build();
        data.setTenantCode(super.getLongValue(node.get("tenantId")));

        QuickBooksDesktopCustomers dbData = super.baseMapper.selectOne(Wrappers.<QuickBooksDesktopCustomers>lambdaQuery()
                .eq(QuickBooksDesktopCustomers::getTenantCode, data.getTenantCode())
                .eq(QuickBooksDesktopCustomers::getListId, data.getListId())
                .last("limit 1"));

        if (ObjectUtils.isEmpty(dbData)){
            super.baseMapper.insert(data);
        }else{
            data.setId(dbData.getId());
            super.baseMapper.updateById(data);
        }
    }
}
